// pages/publishContent/publishContent.js
var that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    item:[],
    id:"",
    zanState:"",//2表示未点赞
    zans:0,
    sends:0,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    console.log(options.transData)
    
    wx.showLoading({
      title: '加载中...',
    })
    setTimeout(() => {
      that.setData({
        id: options.id,
        zanState: options.zanState,
        zans: options.zans,
        sends: options.sends,
      })
      that.getPetshowById()
    }, 1000)

   
  },
  getPetshowById:function(e){
    wx.request({
      url: getApp().globalData.urls + '/wxgz/petshow/getPetshowById',
      data: {
        userId: getApp().globalData.userInfos.id,
        petshowId: that.data.id
      },
      method: "GET",
      header: {
        'content-type': 'application/json' // post:application/x-www-form-urlencoded,get:application/json
      },
      success: function (res) {
        wx.hideLoading()
        console.log("aaaaaaaa", res.data)
        if (res.data.code == 200) {
          var data = res.data.data;

          data.imageUrl = data.imageUrl.split(',');
        
          that.setData({
            imageUrl: data.imageUrl,
            item: data,
            zanState: data.zanState,
            zans: data.zans,
            sends: data.sends,
          })
        }
      },fail:function(e){
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  previewImage:function(e){
    var item = e.currentTarget.dataset.item
   //console.log(that.data.item.imageUrl)
      // 预览图集
      wx.previewImage({
        current:item,
        urls: that.data.item.imageUrl
      });
  },


  //点赞与取消点赞
  saveZanArticle: function () {

    wx.showLoading({
      title: '',
    })
    //console.log(that.data.zanState == 2 ? 1 : 2)
    wx.request({
      url: getApp().globalData.urls + '/wxgz/petshowManage/saveZanPetshow',
      method: "POST",
      data: {
        petshowId: that.data.id,
        token: wx.getStorageSync("token"),
        state: that.data.zanState == 2 ?1:2,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // post:application/x-www-form-urlencoded,get:application/json
      },
      success: function (res) {
        //console.log(res.data)
        wx.hideLoading()
        if (res.data.code == 200) {
          if (that.data.zanState == 2) {
            wx.showToast({
              title: '点赞成功',
            })
          }else{
            wx.showToast({
              title: '取消点赞',
            })
          }
          that.setData({
            zans: that.data.zanState == 2 ? (++that.data.zans) : (--that.data.zans),
            zanState: that.data.zanState == 2 ? 1 : 2,
          })

          
        }else{
          wx.showToast({
            title: res.data.msg,
          })
        }
      },
      fail: function (res) {
        wx.hideLoading()
      }
    })
  },

/**
 *  关联宠物
 */
  details:function(e){
    //console.log(that.data.item.petId)
    wx.request({
      url: getApp().globalData.urls + '/wxgz/pet/getPetInfo',
      method: "GET",
      data: {
        petId: that.data.item.petId
      },
      header: {
        'content-type': 'application/json' // post:application/x-www-form-urlencoded,get:application/json
      },
      success: function (res) {
       // console.log(res.data)
        if(res.data.code == 200){
          wx.navigateTo({
            url: '../petCardInformation/petCardInformation?item=' + JSON.stringify(res.data.data),
          })
        }
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (ops) {
    return {
      //title: that.data.item.title,
      title: that.data.item.userName,
      path: '/pages/articleDetail/articleDetail?id=' + that.data.id,
      imageUrl: that.data.item.imageUrl[0],
      success: function (res) {
        if (ops.from === 'button') {
          // 来自页面内转发按钮
          wx.request({
            url: getApp().globalData.urls + '/wxgz/petshowManage/saveSendPetshow',
            data: {
              petshowId: that.data.id,
              token: wx.getStorageSync("token"),

            },
            method: "POST",
            header: {
              'content-type': 'application/x-www-form-urlencoded' // post:application/x-www-form-urlencoded,get:application/json
            },
            success: function (res) {
              console.log(res.data)

              if (res.data.code == 200) {
                that.setData({
                  sends: (++that.data.sends)
                })
                wx.showToast({
                  title: '转发成功',
                })
              }
            },
            fail: function (res) {

            }
          })
        }
      },
      fail: function (res) {

      }
    }
  },
 

  
})